package com.kelaite.procurement.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.kelaite.procurement.entity.TCompany;
import com.kelaite.procurement.entity.TUser;
import com.kelaite.procurement.service.CompanyService;
import com.kelaite.procurement.service.UserService;
import com.kelaite.procurement.utils.SendMsgWebchinese;
@RestController
public class CellphoneValidateController {
	@Autowired
	private CompanyService companyService;
	@Autowired
	private UserService userService;
	
	@RequestMapping(value="/getValidCode/{cellphone}/{code}",method = RequestMethod.GET)
    public String validateCode( @PathVariable String cellphone,@PathVariable  String code) {
		String result = null;
		try {
			result = SendMsgWebchinese.sendMessage(cellphone, code);
		} catch (Exception e){
			return "-1";
		}
		return result;
	}
	

	@RequestMapping(value="/getAdminValidCode/{companyid}/{phone}/{code}",method = RequestMethod.GET)
    public String paymentCompleted(@PathVariable Long companyid,@PathVariable String phone, @PathVariable  String code) {
		TCompany company = companyService.getObject(companyid);
		TUser user = userService.findByTCompanyAndIsCompanyAdmin(company, 1);
		String cellphone = null;
		if (user != null){
			cellphone = user.getMobilePhone();
		} else {
			cellphone = phone;
		}
		if (cellphone == null){
			return "-1";
		}
		String result = null;
		try {
			result = SendMsgWebchinese.sendMessage(cellphone, code);
		} catch (Exception e){
			return "-1";
		}
		return result;
	}
}
